﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;

namespace Dry.DbInterpreter
{
    public class DbColumn : General.DryBase
    {
        private string _strColumnName;
        private string _strDataType;
        private int _intMaxLength;
        private bool _blnIsNullable;
        private bool _blnIsPK;
        private bool _blnIsFK;
        private bool _blnIsSelectable;
        private bool _blnIsLikeble;
        private bool _blnIsFrom;
        private bool _blnIsTo;
        private string _strReferencedTable;

        public string ColumnName
        {
            get { return _strColumnName; }
            set { _strColumnName = value; }
        }

        public string DataType
        {
            get { return _strDataType; }
            set { _strDataType = value; }
        }

        public int MaxLength
        {
            get { return _intMaxLength; }
            set { _intMaxLength = value; }
        }

        public bool IsNullable
        {
            get { return _blnIsNullable; }
            set { _blnIsNullable = value; }
        }

        public bool IsPK
        {
            get { return _blnIsPK; }
            set { _blnIsPK = value; }
        }

        public bool IsFK
        {
            get { return _blnIsFK; }
            set { _blnIsFK = value; }
        }

        public bool IsSelectable
        {
            get { return _blnIsSelectable; }
            set { _blnIsSelectable = value; }
        }

        public bool IsLikeble
        {
            get { return _blnIsLikeble; }
            set { _blnIsLikeble = value; }
        }

        public bool IsFrom
        {
            get { return _blnIsFrom; }
            set { _blnIsFrom = value; }
        }

        public bool IsTo
        {
            get { return _blnIsTo; }
            set { _blnIsTo = value; }
        }

        public string ReferenceClass
        {
            get { return _strReferencedTable; }
            set { _strReferencedTable = value; }
        }

        public DbColumn(IDataReader dataRecord, string tablePrefix)
        {
            FillMeFromDataRecord(dataRecord, tablePrefix);
        }

        public bool IsIncludedInIP()
        {
            return IsSelectable || IsPK || IsFK || IsLikeble || IsFrom || IsTo;
        }

        public bool ShouldHaveFromAndToParameters()
        {
            return DataType == "nvarchar" ||
                DataType == "int" ||
                DataType == "float" ||
                DataType == "datetime" ||
                DataType == "smalldatetime";
        }

        public bool ShouldHaveLikeParameters()
        {
            return DataType == "nvarchar";
        }

        public bool ShouldHaveInParameters()
        {
            return DataType == "int" || 
                DataType == "float";
        }

        private void FillMeFromDataRecord(IDataRecord dataRecord, string tablePrefix)       
        {
            ColumnName = dataRecord.GetString(dataRecord.GetOrdinal("ColumnName"));
            DataType = dataRecord.GetString(dataRecord.GetOrdinal("DataType"));
            MaxLength = dataRecord.GetInt16(dataRecord.GetOrdinal("MaxLength"));
            IsNullable = dataRecord.GetBoolean(dataRecord.GetOrdinal("IsNullable"));
            IsPK = dataRecord.GetBoolean(dataRecord.GetOrdinal("IsPK"));
            IsFK = dataRecord.GetBoolean(dataRecord.GetOrdinal("IsFK"));
            IsSelectable = dataRecord.GetBoolean(dataRecord.GetOrdinal("IsSelectable"));
            IsLikeble = dataRecord.GetBoolean(dataRecord.GetOrdinal("IsLikeble"));
            IsFrom = dataRecord.GetBoolean(dataRecord.GetOrdinal("IsFrom"));
            IsTo = dataRecord.GetBoolean(dataRecord.GetOrdinal("IsTo"));            
            if (dataRecord.IsDBNull(dataRecord.GetOrdinal("ReferenceTable")))
                ReferenceClass = null;
            else {
                ReferenceClass = dataRecord.GetString(dataRecord.GetOrdinal("ReferenceTable"));
                ReferenceClass = ReferenceClass.Substring(tablePrefix.Length, ReferenceClass.Length - tablePrefix.Length);
            }
        }
    }
}
